<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: luofei614 <weibo.com/luofei614>
// +----------------------------------------------------------------------
namespace Menus;
use think\Session;
use think\Db;

class Menus
{

    //默认配置
    protected $_config = array(
        'AUTH_GROUP' => 'menu_group', // 用户组数据表名
        'AUTH_ACCESS' => 'menu_group_access', // 用户-用户组关系表
        'AUTH_MENU' => 'menu_list', // 权限规则表
        'AUTH_USER' => 'admin_user', // 用户信息表
    );

    public function __construct()
    {
        $option = [
              'AUTH_GROUP' => config('database.prefix') . 'menu_group', // 用户组数据表名
              'AUTH_ACCESS' => config('database.prefix') . 'menu_group_access', // 用户-用户组关系表
              'AUTH_MENU' => config('database.prefix') . 'menu_list', // 权限规则表
            ];
        $this->_config = array_merge($this->_config, $option);
    }
    public function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId =  $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
    public function get_list($uid)
    {
        $groups = $this->getGroups($uid);
         $ids = array(); //保存用户所属用户组设置的所有权限规则id
        foreach ($groups as $g) {
            $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
        }
        $ids = array_unique($ids);
        if (empty($ids)) {
            $_authList[$uid . $t] = array();
            return array();
        }

        $map = array(
            'id' => array('in', $ids),
            'status' => 1,
        );
        //读取用户组所有权限规则
        $lists = Db::table($this->_config['AUTH_MENU'])->where($map)->select();
        $list = $this->list_to_tree($lists,'id','pid','list');
        return $list;
    }
    public function getGroups($uid)
    {
        static $groups = array();
        if (isset($groups[$uid])) {
            return $groups[$uid];
        }

        $user_groups = Db::table($this->_config['AUTH_ACCESS'])
            ->alias('a')
            ->where("a.uid='$uid' and g.status='1'")
            ->join($this->_config['AUTH_GROUP'] . " g", "a.group_id=g.id")
            ->field('uid,group_id,title,rules')->select();
        $groups[$uid] = $user_groups ?: array();
        return $groups[$uid];
    }

 


}
